<template>
  <div class="admin"> 
      <el-card v-loading="pager.loading" class="!border-none" shadow="never">

        <div class="flex justify-between">
        <el-form :inline="true">
          <el-form-item label="账号">
            <el-input
              v-model="queryParams.username"
              placeholder="请输入账号"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getLists">查询</el-button>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="handleAdd">
              <template #icon>
                  <icon name="el-icon-Plus" />
              </template>
              新增
          </el-button>
      </div>

      
          
          <div class="mt-4">
              <el-table :data="pager.lists" size="large">
                  <el-table-column label="ID" prop="id" min-width="60" />>
                  <el-table-column label="头像" min-width="100">
                      <template #default="{ row }">
                          <el-avatar :size="50" :src="row.avatar"></el-avatar>
                      </template>
                  </el-table-column>
                  <el-table-column label="账号" prop="username" min-width="100" />

                  <el-table-column label="创建时间"  min-width="180">
                      <template #default="{ row }"> 
                        {{ row.createdAt }}
                      </template>
                  </el-table-column> 
                    <el-table-column label="最近登录时间"  min-width="180">
                      <template #default="{ row }"> 
                          {{  row.loginTime ? row.loginTime :'--' }}
                      </template>
                    </el-table-column>
                    <el-table-column label="最近登录IP" prop="loginIp" min-width="120" />
                  <el-table-column label="状态" min-width="100" v-perms="['admin/edit']">
                      <template #default="{ row }">
                          <el-switch
                              v-model="row.status"
                              :active-value="1"
                              :inactive-value="0"
                              @change="changeStatus(row)"
                          />  
                      </template>
                  </el-table-column>
                  <el-table-column label="操作" width="130" fixed="right">
                      <template #default="{ row }">
                          <div class="flex items-center">
                            <el-button
                              type="primary"
                              link
                              @click="handleEdit(row)"
                          >
                              编辑
                          </el-button>
                           <!-- <el-button
                              type="primary"
                              link
                              @click="handleDelete(row.id)"
                          >
                              删除
                          </el-button> -->

                          
                          <el-dropdown class="ml-[10px]" >
                            <span class="el-dropdown-link">
                              更多
                              <el-icon class="el-icon--right">
                                <arrow-down />
                              </el-icon>
                            </span>
                            <template #dropdown>
                              <el-dropdown-menu>
                                <el-dropdown-item  @click="handlePassword(row)">重置密码</el-dropdown-item>
                                  <el-dropdown-item  @click="handleDelete(row.id)">删除</el-dropdown-item>  
                              </el-dropdown-menu>
                            </template>
                          </el-dropdown> 


                          </div>
                      </template>
                  </el-table-column>
              </el-table>
          </div> 
      </el-card>
      <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
      <password-popup v-if="showPassword" ref="passwordRef"   @close="showPassword = false" />
  </div>
</template>

<script setup name="admin">
import {ref} from 'vue'
import { getAdminList,adminEditStatus, adminDelete } from '@/apis/system/admin' 
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import PasswordPopup from './password.vue'

const editRef = ref(null)
const showEdit = ref(false)

const showPassword = ref(false)
const passwordRef = ref(null)

  const queryParams = reactive({
    username: '',
  });


const { pager, getLists } = usePaging({
  fetchFun: getAdminList,
    params: queryParams,
})

const handlePassword = async (data) => {
  showPassword.value = true
  await nextTick()  
  passwordRef.value?.open(data)
}

const changeStatus = (data) => {
  adminEditStatus({
      id: data.id, 
      status: data.status,  
  }).finally(() => {
      getLists()
  })
}
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

const handleDelete = async (id) => {
  await feedback.confirm('确定要删除？')
  await adminDelete({ id })
  getLists()
}
 
onMounted(() => {
  getLists()
})
</script>


<style scoped>
.el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
  outline: none;
}
</style>